import { createStore } from 'vuex'
import login from './modules/login'
import menu from './modules/menu'
import { msgCountApi } from '@/api/user'

const userInfo = localStorage.getItem('userInfo')

export default createStore({
    state : () =>  ({
        userInfo : userInfo ? JSON.parse(userInfo) : null,
        msgCount : 0
    }),

    mutations : {
        setUserInfo(state, userInfo) {
            state.userInfo = userInfo
            if(userInfo) {
                localStorage.setItem('userInfo', JSON.stringify(userInfo))
            }else {
                localStorage.removeItem('userInfo')
            }
        },

        setMsgCount(state, count) {
            state.msgCount = count
        }
    },

    actions : {
        async msgCount({commit}) {
            const count = await msgCountApi()
            commit('setMsgCount', count)
        }
    },

    modules : {
        menu : menu,
        login : login
    },
})